import { Injectable } from "@angular/core";
import { BaseHttpService } from "../../../../shared/services/baseHttp.service";
import { Observable } from "rxjs/Observable";
import { HttpClient, HttpParams } from "@angular/common/http";
import { ControversyQualitativePerformanceModel, ControversyEventIndicatorModel } from "../../../../shared/models/qualitativePerformance.model";
import { IncidentLocationModel } from "./models/incidentLocation.model";
import { IncidentDetailsModel } from "./models/incidentDetails.model";
import { ReplaySubject } from "rxjs";
import { ControversyGroupModel } from "./components/controversiesIncidents/models/controversyGroup.model";

/**
 * Company controversies service
 * 
 * Author: Sorin Spiru
 * Date: 01.03.2018
 */
@Injectable()
export class CompanyControversiesService extends BaseHttpService {

    public themeSubject = new ReplaySubject<ControversyQualitativePerformanceModel[]>(1);
    public controviersiesTheme = this.themeSubject.asObservable();

    private comparisonSubject = new ReplaySubject<ControversyGroupModel[]>(1);
    public comparison = this.comparisonSubject.asObservable();

    constructor(public http: HttpClient) {
        super();
    }

    /**
     * Get company controversies by theme
     * @param companyId Company unique identifier
     * @param theme Theme to search by
     */
    public getControversiesByTheme(companyId: string, theme: string = ''): ReplaySubject<ControversyQualitativePerformanceModel[]> {
        let params: HttpParams = new HttpParams();
        params = params.set('theme', theme);
        this.http.get<ControversyQualitativePerformanceModel[]>(this.serverUrl + 'controversiesserv/api/v1/company/' + companyId + '/controversyIndicator', { params: params }).subscribe(data=>{
            this.themeSubject.next(data);
        });

        return this.themeSubject;
    }

    /**
     * Get company's incident locations
     * @param companyId Company unique identifier
     */
    public getIncidentsLocation(companyId: string): Observable<IncidentLocationModel[]> {
        let params: HttpParams = new HttpParams();
        params = params.set('IncludeHQ', 'true')
            .set('Location', 'true')
            .set('Type', '0');

        return this.http.get<IncidentLocationModel[]>(this.serverUrl + 'controversiesserv/api/v1/company/' + companyId + '/controversyIndicator/eventIndicator/incident', { params: params });
    }

    /**
     * Get controversy event indicator
     * @param companyId Company unique identifier
     * @param eventId Incident unique identifier
     */
    public getEventIndicator(companyId: string, eventId: string): Observable<ControversyEventIndicatorModel>{
        return this.http.get<ControversyEventIndicatorModel>(this.serverUrl + 'controversiesserv/api/v1/company/' + companyId +
            '/controversyIndicator/00000000-0000-0000-0000-000000000000/eventIndicator/' + eventId);
    }

    /**
     * Get incident's location
     * @param companyId Company unique identifier
     * @param eventId Incident unique identifier
     */
    public getIncidentLocations(companyId: string, eventId: string): Observable<IncidentLocationModel[]> {
        let params: HttpParams = new HttpParams();
        params = params.set('IncludeHQ', 'false')
            .set('Location', 'true')
            .set('Type', '0');

        return this.http.get<IncidentLocationModel[]>(this.serverUrl + 'controversiesserv/api/v1/company/' + companyId +
            '/controversyIndicator/' + companyId + '/eventIndicator/' + eventId + '/incident', { params: params });
    }

    /**
     * Get controversy incident indicators
     * @param companyId Company unique identifier
     * @param eventId Incident unique identifier
     */
    public getIncidents(companyId: string, eventId: string): Observable<IncidentDetailsModel[]> {
        return this.http.get<IncidentDetailsModel[]>(this.serverUrl + 'controversiesserv/api/v1/company/' + companyId +
            '/controversyIndicator/00000000-0000-0000-0000-000000000000/eventIndicator/' + eventId + '/incident');
    }

    /**
     * Get controversies comparison for group
     * @param companyId Company unique identifier
     */
    public getComparisonGroupControversies(companyId: string): ReplaySubject<ControversyGroupModel[]> {
        let params: HttpParams = new HttpParams();
        params = params.set('level', '0');
        this.http.get<ControversyGroupModel[]>(this.serverUrl + 'controversiesserv/api/v1/company/' + companyId + '/controversyIndicator/' + companyId + '/comparisonGroupControversies', { params: params }).subscribe(data=>{
            this.comparisonSubject.next(data);
        });

        return this.comparisonSubject;
    }
}